﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

///<summary>
/// Class attached to all ennemies
///</summary>
public class EnnemyBehaviour : MonoBehaviour {

    [SerializeField]
    private float height = 10;
    [SerializeField]
    private float speed;
    [SerializeField]
    private float health = 100;
    [SerializeField]
    private int income = 5;
    [SerializeField]
    private int score = 10;
    [SerializeField]
    private int xp = 10;

    [SerializeField]
    public ParticleEmitter smokeTrail;
    [SerializeField]
    public GameObject explosionEffect;

    private GameObject[] path;

    private PathManager pathManager;
    private int pathIndex = 0;

    private Vector3 nextPosition;

    private float maxHealth;
    private bool dead = false;

    public float Height
    {
        get { return height; }
        set { height = value; }
    }

    public float Speed
    {
        get { return speed; }
        set { speed = value; }
    }

    public float Health
    {
        get { return health; }
        set { health = value; }
    }

    public int Income
    {
        get { return income; }
        set { income = value; }
    }

    public int Xp
    {
        get { return xp; }
        set { xp = value; }
    }

    public int Score
    {
        get { return score; }
        set { score = value; }
    }

    public GameObject[] Path
    {
        get { return path; }
        set { path = value; }
    }

    public void setPath(GameObject[] path)
    {
        Path = path;
    }


	// Use this for initialization
	public void Start () 
    {
        //Save the max health
        maxHealth = health;
        //Place the transform where is has to (air or ground)
        transform.Translate(0, height, 0);
        nextPosition = path[pathIndex].transform.position;
	}
	
	// Update is called once per frame
	public void Update () 
    {
        //Move the ennemy
        transform.LookAt(nextPosition);
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
        if (Vector3.Distance(transform.position, nextPosition) < 0.5)
        {
            if (pathIndex != path.Length - 1)
            {
                pathIndex++;
            }
            nextPosition = path[pathIndex].transform.position;
        }
	}

    public void applyDamages(float dmg)
    {
        if (!dead)
        {
            health -= dmg;
            EventManager<GameObject, int>.Raise("enemy recieve dmg", gameObject, (int)dmg);
            //Destroy the object if its health is negative
            if (health <= 0)
            {
                playerKill();
                return;
            }
            else if (health / maxHealth <= 0.75 && smokeTrail)
            {
                //Apply smokeTrail if it is damaged
                smokeTrail.emit = true;
            }
        }
    }

    public void playerKill()
    {
        dead = true;
        EventManager<GameObject>.Raise("ennemy killed", collider.gameObject);
        EventManager<int, Vector3>.Raise("xp gain", Xp, transform.position);
        if (explosionEffect)
        {
            Instantiate(explosionEffect, transform.position, Quaternion.identity);
        }
        Destroy(gameObject);
    }
}
